﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SonCa.SocialNetwork.Profiles.IPresenters;
using SonCa.SocialNetwork.Profiles.Presenters;
using Core.DataAccess;
using System.ComponentModel.Composition;
using Core.BusinessLogic.Interfaces;
using Core.Common;
using Core.Interfaces;
using SonCa.SocialNetwork.Web.ASCX;
using SonCa.SocialNetwork.Web.HelperClass;
using SonCa.SocialNetwork.Web;
using SonCa.SocialNetwork.Web.Chat.Presenters;

namespace SonCa.SocialNetwork.Profiles
{
    public partial class Profile : System.Web.UI.Page, IProfilePresenter, IUploadAvatar
    {
        private ProfilePresenter _presenter;
        private UploadAvatarPresenter _presenterUpload;        

        [Import]
        private IUserSession _userSession;
        [Import]
        private IWebContext _webContext;

        public Profile()
        {
            MEFManager.Compose(this);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            _presenter = new ProfilePresenter();
            string username = Page.RouteData.Values["username"] as string;            
            _presenter.Init(this,username);

            if (UserChatPresenter.addCurrentID)
            {
                UserChatPresenter.AddUserChat("", _userSession.CurrentUser.AccountID.ToString(), _userSession.UserName, HttpContext.Current.Request.Cookies["ASP.NET_SessionId"].Value.ToString());                                
            }            
        }

        public void DisplayInfo(Account account)
        {
            lblProfileName.Text = string.IsNullOrEmpty(account.LastName) ? "" : string.IsNullOrEmpty(account.MidName) ? account.LastName : string.IsNullOrEmpty(account.FirstName) ? account.LastName + " " + account.MidName : account.LastName + " " + account.MidName + " " + account.FirstName;
        }


        public void SetAvatar(int AccountID)
        {
            if (!imgAvatar.ImageUrl.Contains("AccountID="))
                imgAvatar.ImageUrl += "?AccountID=" + AccountID.ToString();
        }

        protected void btnSubmitEditAvatar_Click(object sender, EventArgs e)
        {
            if (fuAvatarUpload.PostedFile != null || !string.IsNullOrEmpty(fuAvatarUpload.PostedFile.FileName))
            {
                _presenterUpload.UploadFile(fuAvatarUpload.PostedFile, Request.Url.ToString());
            }
        }

        protected void pnlUpload_Load(object sender, EventArgs e)
        {
            _presenterUpload = new UploadAvatarPresenter();            
            _presenterUpload.Init(this);            
        }


        //public void LoadFriends(List<FriendsProfile> contentObj)
        //{
        //    rptFriend.DataSource = contentObj;
        //    rptFriend.DataBind();
        //}

        //protected void repFriends_ItemDataBound(object sender, RepeaterItemEventArgs e)
        //{
        //    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        //    {
        //        ProfileDisplay pdProfileDisplay = e.Item.FindControl("pdProfileDisplay") as ProfileDisplay;
        //        pdProfileDisplay.LoadDisplay(((FriendsProfile)e.Item.DataItem));
        //    }
        //}

        public void ShowMessage(string Message)
        {
            //lblMessage.Text = Message;
            Page.ClientScript.RegisterClientScriptBlock(GetType(), "message", "alert('"+ Message +"');", true);
        }

        public void ShowCropPanel()
        {
            pnlUpload.Visible = false;
        }

        public void ShowApprovePanel()
        {
            pnlUpload.Visible = false;
        }

        public void ShowUploadPanel()
        {
            pnlUpload.Visible = true;
        }

        public void ShowAvatar(int accountID)
        {
            imgAvatar_popup.ImageUrl = "~/Images/ProfileAvatar/ProfileImage.aspx?AccountID=" + accountID.ToString();            
        }

        public void CloseDialog()
        {
            Page.ClientScript.RegisterClientScriptBlock(GetType(), "close", "closeDialog();", true);
        }

        public void ShowInfo(Account account, WorkPlace workPlace)
        {

        }       
    }
}